package com.courseware.online.service;

import com.courseware.online.common.CommonUtil;
import com.courseware.online.config.AppConfig;
import com.courseware.online.dao.CoursewareDao;
import com.courseware.online.dao.PageDao;
import com.courseware.online.dto.CoursewareDTO;
import com.courseware.online.entity.Courseware;
import com.courseware.online.entity.Page;
import com.courseware.online.vo.CoursewareDetailVO;
import com.courseware.online.vo.CoursewareVO;
// import com.courseware.online.vo.PageInfo;
import com.courseware.online.vo.PageVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xslf.usermodel.*;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author demos
 */
@Slf4j
@Service
public class CoursewareServiceImpl implements CoursewareService {
    @Autowired
    private CoursewareDao coursewareDao;
    @Autowired
    private PageDao pageDao;
    @Autowired
    private Mapper dozerMapper;
    @Autowired
    private AppConfig appConfig;
    @Autowired
    private CommonUtil commonUtil;

    @Override
    public PageInfo<CoursewareVO> getCoursewareByPage(CoursewareDTO dto) {
        PageHelper.startPage(dto.getPageNumber(), dto.getPageSize());
        List<Courseware> coursewares = coursewareDao.getCoursewareList(dto.getName());
        if (coursewares == null) {
            coursewares = new ArrayList<>();
        }
        PageInfo<Courseware> coursewarePageInfo= new PageInfo<>(coursewares, dto.getPageSize());
        PageInfo<CoursewareVO> coursewareVOPageInfo = new PageInfo<>();
        coursewareVOPageInfo.setList(CommonUtil.mapList(dozerMapper, coursewarePageInfo.getList(), CoursewareVO.class));
        coursewareVOPageInfo.setTotal(coursewarePageInfo.getTotal());
        coursewareVOPageInfo.setPageNum(coursewarePageInfo.getPageNum());
        coursewareVOPageInfo.setPages(coursewarePageInfo.getPages());
        return coursewareVOPageInfo;
    }

    @Override
    public CoursewareDetailVO getCoursewareDetail(int id) {
        CoursewareDetailVO vo = new CoursewareDetailVO();
        Courseware courseware = coursewareDao.getCoursewareById(id);
        List<Page> pages = pageDao.getPageList(id);
        vo.setName(courseware.getName());
        vo.setUrl(courseware.getUrl());
        if (!CollectionUtils.isEmpty(pages)) {
            vo.setPages(CommonUtil.mapList(dozerMapper, pages, PageVO.class));
        }
        return vo;
    }

    @Override
    public void addCourseware(CoursewareDetailVO vo) {
        changeToPPT(vo);
        Courseware courseware = dozerMapper.map(vo, Courseware.class);
        courseware.setUrl(appConfig.getDownloadUrl() + vo.getName());
        coursewareDao.createCourseware(courseware);
        List<Page> pages = new ArrayList<>();
        for (PageVO pageVO : vo.getPages()) {
            pageVO.setCoursewareId(courseware.getId());
            pages.add(dozerMapper.map(pageVO, Page.class));
        }
        pageDao.insertBatch(pages);
    }

    @Override
    public void updateCourseWare(CoursewareDetailVO vo) {
        // 查找到原来的PPT信息
        Courseware oldCourseware = coursewareDao.getCoursewareById(vo.getId());
        // 删除原来的PPT
        File oldppt = new File(commonUtil.getFilePath(oldCourseware.getName()));
        if (oldppt.exists()) {
            oldppt.delete();
        }
        // 生成新的PPT
        changeToPPT(vo);
        pageDao.deleteByCoursewareId(oldCourseware.getId());
        List<Page> pages = CommonUtil.mapList(dozerMapper, vo.getPages(), Page.class);
        pageDao.insertBatch(pages);
        // 保存课件信息
        oldCourseware.setName(vo.getName());
        oldCourseware.setUrl(appConfig.getDownloadUrl() + vo.getName());
        coursewareDao.updateById(oldCourseware);


    }

    @Override
    public List<CoursewareVO> getCoursewareByName(String name) {
        List<Courseware> courseware = coursewareDao.getCoursewareList(name);
        if (!CollectionUtils.isEmpty(courseware)) {
            return CommonUtil.mapList(dozerMapper, courseware, CoursewareVO.class);
        }
        return new ArrayList<>();
    }

    @Override
    public int delCourseware(Integer id) {
        // 查找到原来的PPT信息
        Courseware oldCourseware = coursewareDao.getCoursewareById(id);
        // 删除原来的PPT
        File oldppt = new File(commonUtil.getFilePath(oldCourseware.getName()));
        if (oldppt.exists()) {
            oldppt.delete();
        }
        return coursewareDao.delete(id);
    }

    /**
     * 生成PPT的方法
     * @param vo 页面上传过来的PPT对象
     */
    private void changeToPPT(CoursewareDetailVO vo) {
        // 新建一个PPT
        XMLSlideShow ppt = new XMLSlideShow();
        //设置ppt母版，SlideLayout.TITLE_AND_CONTENT表示页面上有标题和内容。
        XSLFSlideLayout titleLayout = ppt.getSlideMasters()[0].getLayout(SlideLayout.TITLE_AND_CONTENT);
        // 循环遍历页面上添加的幻灯片，使用 ppt.createSlide 方法在ppt里生成一张幻灯片，并设置 标题title 和 内容content
        for (PageVO page : vo.getPages()) {
            //创建ppt的一张幻灯片
            XSLFSlide newSlide = ppt.createSlide(titleLayout);
            // 设置标题
            XSLFTextShape title = newSlide.getPlaceholder(0);
            title.setText(page.getTitle());
            // 设置内容
            XSLFTextShape content = newSlide.getPlaceholder(1);
            content.setText(page.getContent());
        }
        // 将PPT保存下来，保存路径为 项目所在目录+download
        File file=new File(commonUtil.getFilePath(vo.getName()));
        try {
            FileOutputStream out = new FileOutputStream(file);
            ppt.write(out);
            out.close();
        } catch (IOException e) {
            log.error("保存PPT出错", e);
        }
    }
}
